package com.zhijiaoyun.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.ResponseUtils;
import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.dao.ICourseCategoryDao;
import com.zhijiaoyun.model.CourseCategory;
import com.zhijiaoyun.service.ICourseCategoryService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "courseCategoryService")
public class CourseCategoryServiceImpl implements ICourseCategoryService {

	@Resource
	ICourseCategoryDao courseCategoryDao;

	@Override
	public CourseCategory getCourseCategoryByName(String name) {
		return courseCategoryDao.getCourseCategoryByName(name);
	}

	@Override
	public void addCategory(CourseCategory courseCategory) {
		courseCategoryDao.insert(courseCategory);
	}

	@Override
	public CourseCategory getCourseCategoryByNameNotCategoryId(String name, Long categoryId) {
		return courseCategoryDao.getCourseCategoryByNameNotCategoryId(name, categoryId);
	}

	@Override
	public void updateCategory(CourseCategory courseCategory) {
		courseCategoryDao.updateByPrimaryKey(courseCategory);
	}

	@Override
	public CourseCategory getCourseCategoryByCategoryId(Long categoryId) {
		CourseCategory courseCategory = courseCategoryDao.selectByPrimaryKey(categoryId);
		return courseCategory;
	}

	@Override
	public List<CourseCategory> getCourseCategory() {
		return courseCategoryDao.listAll();
	}

	@Override
	public List<CourseCategory> getCourseCategoryByParentId(Long parentId) {
		long id = parentId == null ? 0 : parentId;
		List<CourseCategory> categories = courseCategoryDao.listByParentId(id);
		return categories;
	}

	@Override
	public List<Long> getIdsByParentId(Long categoryId) {
		return courseCategoryDao.selectIdsByParentId(categoryId);
	}

	@Override
	public List<Long> getLastIdsByCategoryId(Long categoryId, List<Long> categoryIds) {
		categoryId = categoryId == null ? 0 : categoryId;
		List<Long> ids = courseCategoryDao.selectIdsByParentId(categoryId);
		if (StringUtils.isNotEmptyList(ids)) {
			for (Long long1 : ids) {
				getLastIdsByCategoryId(long1, categoryIds);
			}
		} else {
			categoryIds.add(categoryId);
		}
		return categoryIds;
	}

	@Override
	public CourseCategory getCourseCategoryByNameParentId(String name, Long parentId) {
		return courseCategoryDao.selectByNameParentId(name, parentId);
	}

	@Override
	public CourseCategory getCourseCategoryByNameNotCategoryId(String name, Long parentId, Long categoryId) {
		return courseCategoryDao.selectByNameParentIdNotCategoryId(name, parentId, categoryId);
	}

	@Override
	public void deleteCourseCategory(CourseCategory category) {
		courseCategoryDao.deleteByPrimaryKey(category.getCategoryId());
	}

	@Override
	public void deleteCourseCategoryByParentId(Long parentId) {
		courseCategoryDao.deleteByParentId(parentId);
	}

	@Override
	public List<Long> getSpecialIdsByParentId(Long parentId, boolean isHigerEdu) {
		return courseCategoryDao.selectSpecialIdsByParentId(parentId, isHigerEdu);
	}

	@Override
	public List<Long> getSpecialIdsByParentIdType(Long parentId, int type) {
		return courseCategoryDao.selectSpecialIdsByParentIdType(parentId, type);
	}

	@Override
	public Map<String, Object> listByParentId(long parentId) {
		List<CourseCategory> categorys = getCourseCategoryByParentId(parentId);
		Map<String, Object> map = new HashMap<>();
		map.put("list", categorys);
		return ResponseUtils.responseSuccessMap(map);
	}
}
